/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.frontend.animal;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.tos.AnimalTo;
import pa165.dominating.species.tos.EnvironmentTo;
import pa165.dominatingspecies.frontend.pages.BasePage;
import pa165.dominatingspecies.frontend.security.Authorizer;
import pa165.dominatingspecies.service.impl.AnimalServiceImpl;
import pa165.dominatingspecies.service.impl.EnvironmentServiceImpl;

/**
 *
 * @author Filip Dinh
 */
@PreAuthorize("hasRole('WORKER')")
public class AnimalsPage extends BasePage {

    @SpringBean(name = "animalService")
    private AnimalServiceImpl animalService;
    private EnvironmentTo selectedEnvironment = null;
    @SpringBean(name = "environmentService")
    private EnvironmentServiceImpl environmentService;

    public AnimalsPage() {
        initGui();
    }

    public AnimalsPage(EnvironmentTo environment) {
        selectedEnvironment = environment;
        initGui();
    }

    private void initGui() {
        addAnimalsModule();

        addNewAnimalLink();

    }

    private void addAnimalsModule() {
        ListView<AnimalTo> listView = new ListView<AnimalTo>("listview", createModelForAnimals()) {
            @Override
            protected void populateItem(ListItem<AnimalTo> item) {
                item.add(new Label("id", new PropertyModel(item.getModel(), "id")));
                final AnimalTo animal = (AnimalTo) item.getModelObject();
                item.add(new Label("name", new PropertyModel(item.getModel(), "name")));
                item.add(new Label("species", new PropertyModel(item.getModel(), "species")));


                Link<BasePage> edit = new Link<BasePage>("edit") {
                    @Override
                    public void onClick() {
                        EditAnimalPage animalPage = new EditAnimalPage(animal.getId(), selectedEnvironment);
                        setResponsePage(animalPage);
                    }
                };
                item.add(edit);

                Link deleteLink = new DeleteAnimalLink("delete", item.getModelObject(), selectedEnvironment);
                Authorizer.authorize(deleteLink);
                item.add(deleteLink);

                Link<BasePage> prey = new Link<BasePage>("prey") {
                    @Override
                    public void onClick() {
                        AnimalPreyPredatorsPage app = new AnimalPreyPredatorsPage(animal.getId(), selectedEnvironment, true);
                        setResponsePage(app);
                    }
                };
                item.add(prey);

                Link<BasePage> predator = new Link<BasePage>("predator") {
                    @Override
                    public void onClick() {
                        AnimalPreyPredatorsPage app = new AnimalPreyPredatorsPage(animal.getId(), selectedEnvironment, false);
                        setResponsePage(app);
                    }
                };
                item.add(predator);
            }
        };
        add(listView);
        Form<AnimalsPage> filterForm = new Form<>("AnimalFilterForm",
                new CompoundPropertyModel<>(this));
        Button submitButton = new Button("submitButton") {
            @Override
            public void onSubmit() {
                if (selectedEnvironment == null) {
                    getSession().warn(new StringResourceModel("animalFilterWarn", this, null).getString());
                } else {
                    setResponsePage(new AnimalsPage(selectedEnvironment));
                }


            }
        };
        filterForm.add(submitButton);
        Button cancelButton = new Button("cancelButton") {
            @Override
            public void onSubmit() {
                setResponsePage(new AnimalsPage(null));

            }
        };
        if (selectedEnvironment == null) {
            cancelButton.setVisible(false);
        }
        filterForm.add(cancelButton);
        filterForm.add(new Label("environmentsLabel", new StringResourceModel("selectFilter", this, null)));
        filterForm.add(new Label("filterLabel", new StringResourceModel("selectedFilter", this, null)));
        if (selectedEnvironment == null) {
            filterForm.add(new Label("filterName", new StringResourceModel("noneFilter", this, null)));
        } else {
            filterForm.add(new Label("filterName", selectedEnvironment.toString()));
        }

        List<EnvironmentTo> environments = environmentService.getAllEnvironments();
        filterForm.add(new DropDownChoice<>("environmentsx", new PropertyModel<EnvironmentTo>(this, "selectedEnvironment"), environments));
        add(filterForm);



    }

    private LoadableDetachableModel<List<AnimalTo>> createModelForAnimals() {

        return new LoadableDetachableModel<List<AnimalTo>>() {
            @Override
            protected List<AnimalTo> load() {
                List<AnimalTo> list = new ArrayList<>();
                if (selectedEnvironment == null) {
                    list = animalService.getAllAnimals();
                } else {
                    list = animalService.getAnimalsByEnvironment(selectedEnvironment);
                }

                return list;
            }
        };
    }

    private void addNewAnimalLink() {
        Link addAnimalLink = new Link("createAnimalLink") {
            @Override
            public void onClick() {
                setResponsePage(new CreateAnimalPage(selectedEnvironment));
            }
        };
        Authorizer.authorize(addAnimalLink);
        add(addAnimalLink);
    }

    public void setAnimalService(AnimalServiceImpl animalService) {
        this.animalService = animalService;
    }

    public EnvironmentTo getSelectedEnvironment() {
        return selectedEnvironment;
    }

    public void setSelectedEnvironment(EnvironmentTo selectedEnvironment) {
        this.selectedEnvironment = selectedEnvironment;
    }
}
